<?php
namespace Admin\Controller;

use Think\Controller;

class PHPExcelController extends Controller
{
    /**方法**/
    public function  index()
    {
        /*        $xlsModel = M();
                $sql = "SELECT th_user.*, th_survey.* FROM th_user JOIN th_survey ON th_user.id = th_survey.user_id";
                $User = $xlsModel->query($sql);

                dump($User);*/
        $this->display();
    }

    public function exportExcel($expTitle, $expCellName, $expTableData)
    {
        ob_clean();
        $xlsTitle = iconv('utf-8', 'gb2312', $expTitle);//文件名称
        $fileName = 'users' . date('_YmdHis');//or $xlsTitle 文件名称
        $cellNum = count($expCellName);
        $dataNum = count($expTableData);
        vendor('PHPExcel.PHPExcel', '', '.php');
        vendor('PHPExcel.PHPExcel.IOFactory', '', '.php');
        $objPHPExcel = new \PHPExcel();
        $cellName = array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'AA', 'AB', 'AC', 'AD', 'AE', 'AF', 'AG', 'AH', 'AI', 'AJ', 'AK', 'AL', 'AM', 'AN', 'AO', 'AP', 'AQ', 'AR', 'AS', 'AT', 'AU', 'AV', 'AW', 'AX', 'AY', 'AZ');

        $objPHPExcel->getActiveSheet(0)->mergeCells('A1:' . $cellName[$cellNum - 1] . '1');//合并单元格
        // $objPHPExcel->setActiveSheetIndex(0)->setCellValue('A1', $expTitle.'  Export time:'.date('Y-m-d H:i:s'));
        for ($i = 0; $i < $cellNum; $i++) {
            $objPHPExcel->setActiveSheetIndex(0)->setCellValue($cellName[$i] . '2', $expCellName[$i][1]);
        }
        // Miscellaneous glyphs, UTF-8
        for ($i = 0; $i < $dataNum; $i++) {
            for ($j = 0; $j < $cellNum; $j++) {
                $objPHPExcel->getActiveSheet(0)->setCellValue($cellName[$j] . ($i + 3), $expTableData[$i][$expCellName[$j][0]]);
            }
        }

        header('pragma:public');
        header('Content-type:application/vnd.ms-excel;charset=utf-8;name="' . $xlsTitle . '.xls"');
        header("Content-Disposition:attachment;filename=$fileName.xls");//attachment新窗口打印inline本窗口打印
        $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
        $objWriter->save('php://output');
        exit;
    }

    /**
     *
     * 导出Excel
     */
    public function expUser()
    {//导出Excel
        $xlsName = "user";
        $xlsCell = array(
            array('id', 'id'),
            array('username', '姓名'),
            array('email', '邮箱'),
            array('phone', '手机号'),
            array('updatetime', '更新时间'),
            array('q1', 'q1'),
            array('q2', 'q2'),
            array('q3', 'q3'),
            array('q4', 'q4'),
            array('createtime', '创建时间'),
        );


        $xlsModel = M();
        $sql = "SELECT th_user.*, th_survey.* FROM th_user JOIN th_survey ON th_user.id = th_survey.user_id";
        $User = $xlsModel->query($sql);

        $xlsData = $User;/*->Field('id,username,phone,isVIP,notVIP,chanel,supermarket,bank,website,othersload,themeparty,faceactivity,getchanel,professional,goodman,personalparty,culture,othermessage')*/

        //dump($User);
        /*foreach ($xlsData as $k => $v) {
            $xlsData[$k]['sex'] = $v['sex'] == 1 ? '男' : '女';
        }*/
        $this->exportExcel($xlsName, $xlsCell, $xlsData);
    }
}